#include "form_send.h"
#include "ui_form_send.h"
#include <QDebug>
#include <QStringList>
#include <QFile>
#include <QTime>
#include <QCoreApplication>
#include <QEventLoop>
#include "config.h"
extern QString LOGIN;
extern QString DELIVERY;
extern QString PICKUP;
extern QString STATUS;
extern QString BIN_NUM;
extern QString ServerIp;
extern QString Port;

QString qrcode = "E:\\qrcode.jpg";
QString courier_send = "./record/courier_send.txt";
QString client_pickup = "./record/client_pickup.txt";


Form_send::Form_send(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_send)
{
    ui->setupUi(this);

    auto_or_manual = 0;
    pix_len = 0;
    count_sure = 0;

    connect(&fbs, SIGNAL(return_send()), this, SLOT(box_to_send_Slot()));

    socket = new QTcpSocket(this);

    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readSlot()));

    camera = new QCamera(this);
    viewfinder = new QCameraViewfinder(this);
    imageCapture = new QCameraImageCapture(camera);

    connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(displayImage(int,QImage)));

}

Form_send::~Form_send()
{
    delete ui;
}
void Form_send::Sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
int Form_send::judge_phone_num()
{
    qDebug() << "check two times phone num, rts";
    QString first_phn = ui->first->text();
    QString again_phn = ui->again->text();
    if(0 == first_phn.compare(again_phn) && first_phn.length() == 11)
    {
        PHONE_CLIENT = again_phn;
        return 0;
    }
    else
    {
        QMessageBox::warning(this, "send", "please check phone num");

        return -1;
    }
}

/* connection */
void Form_send::new_connection()
{
    socket->abort();//abort connected
    socket->connectToHost(ServerIp, 8888);
}
/* open camera */
void Form_send::on_btn_scan_clicked()
{
    //camera->setViewfinder(viewfinder);
    auto_or_manual = 1;
    camera->start();
    imageCapture->capture();
}
/* fetch image and show */
void Form_send::displayImage(int , QImage image)
{
    image=image.convertToFormat(QImage::Format_RGB888);
    QPixmap pixmap = QPixmap::fromImage(image);
    pixmap.scaled(ui->label_photo->size(), Qt::KeepAspectRatio);

    ui->label_photo->setScaledContents(true);
    ui->label_photo->setPixmap(pixmap);

    QImage::Format format =  image.format();
    qDebug() << (int)format;
}
/* auto connect save */
void Form_send::on_btn_photo_clicked()
{
    if(auto_or_manual != 1)
        return ;
    const QPixmap *pixmap = ui->label_photo->pixmap();
    if(pixmap)
    {
         pixmap->save(qrcode);
         qDebug()<<"save success"<<endl;
         camera->stop();
         new_connection();
    }
}

void Form_send::read_photo()
{
    qDebug() << "read_photo send qrcode";
#if 0
    QFile file_photo(qrcode);
    pix_len = file_photo.size();
    qDebug() << "pix_len size : " << pix_len;

    file_photo.open(QIODevice::ReadOnly);
    info_photo.clear();
    info_photo = file_photo.readAll();
    qDebug() << "info_photo size : " << info_photo.size();
#endif

    QByteArray ba = qrcode.toLatin1(); // must

    QPixmap pix(tr(ba.data()));


    buffer.open(QIODevice::ReadWrite);
    pix.save(&buffer, "jpg");

    pix_len = (quint32)buffer.data().size();
    qDebug("image size:%d",pix_len);

}

/* send qrcode */
void Form_send::auto_photo_send()
{
#if 0
    QString send;
    send.sprintf("%s#%s#%s#", DELIVERY.toStdString().data(), BIN_NUM.toStdString().data()\
                 , "auto");
    qDebug() << "send photo :" << send;
    qDebug() << "before write photo length:" << send.length();

    QByteArray arr;
    arr.clear();
    arr.append(send);
    arr.append(buffer.data());

    QFile file_photo(qrcode);
    file_photo.open(QIODevice::ReadOnly);
    info_photo.clear();
    info_photo = file_photo.readAll();
    qDebug() << "info_photo size : " << info_photo.size();

    qDebug() << "write photo length:" << socket->write(info_photo);
#endif
    Sleep(1000);
    QByteArray arr;
    arr.clear();
    arr.append(buffer.data());
    //arr.append("#");
    qDebug() << "write photo length:" << socket->write(arr);
    Sleep(1000);
    qDebug() << "fdfsfs";
}

/* auto send */
void Form_send::send_qrcode_request_auto()
{
    read_photo();
    /* send auto request and length */
    QString send;
    send.sprintf("%s#%s#%s#%u#", DELIVERY.toStdString().data(), BIN_NUM.toStdString().data()\
                 , "auto", pix_len);
    QByteArray arr;
    arr.clear();
    arr.append(send);

    qDebug() << "auto request length:" << socket->write(arr);
    qDebug() << send;
    pix_len = 0;

}


void Form_send::connectedSlot()
{
    qDebug() << "connected success";
    if(auto_or_manual == 0 && count_sure == 3) // manual mode
        send_send_request();
    else if(auto_or_manual == 1)   // auto mode
    {
            send_qrcode_request_auto();
            if(last.toInt() == -2)
            {
                qDebug() << "又一次建立了连接，发送图片";
                auto_photo_send();
                auto_or_manual = 0;
            }
    }
    clearall();
    qDebug() << "send send";
}

void Form_send::readSlot()
{
    qDebug() << "read readly";
    read_send_request();
    qDebug() << "read send";
}
/* get manual request info */
int Form_send::get_info(QString recv)
{
    if(recv.length() == 0)
        return -1;
    QStringList list;
    list = recv.split("#");
    qDebug() << "list->length()" << list.length();// list->length();

    int i;
    for(i = 0; i < 3; i++)
    {
        qDebug() << "list->at(" << i << "):" << list.at(i);
    }
    QString tmp = list.last();
    QStringList list2;
    list2 = tmp.split("\r");
    last = list2.first();
    qDebug()<<last;
    return 0;
}
/* read manual request */
void Form_send::read_send_request()
{
    QByteArray arr;
    QString recv;

    arr.clear();
    arr = socket->readAll();
    recv = QString(arr);

    get_info(recv);

    qDebug()<< "last=last=last=" << last;
    if(last.toInt() >= 0)
    {
        qDebug() << "send send success, next interface";
        this->hide();
        fbs.show();
        QString label_show = last + " box is opening, please fetch pack";
        if(last.toInt() == 0)
        {
            LED1_ON;
            qDebug()<<"led1 on";
        }
        else if(last.toInt() == 1)
        {
            LED2_ON;
            qDebug()<<"led2 on";
        }
        fbs.ui->label_show->setText(label_show);
    }
#if 1
    else if(last.toInt() == -2) // 可以发送图片
    {
        new_connection();
        qDebug() << "建立了新的连接";
        //auto_photo_send();
    }
#endif
    else if(last.toInt() == -3) // 没有空格子
    {
        QMessageBox::warning(this, "send", " No Spaces !");
        clearall();
        qDebug() << "No Spaces!";
    }
    else
    {
        QMessageBox::warning(this, "send", " error!");
        clearall();
        qDebug() << "fail send";
    }
}
/* manual send */
void Form_send::send_send_request()
{
    judge_phone_num();
    QString ORDER_NUM = ui->order_num->text();

    QString send;
    send.sprintf("%s#%s#%s#%s#", DELIVERY.toStdString().data(), BIN_NUM.toStdString().data()\
                 , PHONE_CLIENT.toStdString().data(), ORDER_NUM.toStdString().data());
    QByteArray arr;
    arr.clear();
    arr.append(send);

    qDebug() << "send length:" << socket->write(arr);

    qDebug() << send;
    clearall();
}
/* manual connection */
void Form_send::on_enter_clicked()
{
    count_sure++;
    if(count_sure == 3)
    {
        int k = judge_phone_num();
        if(k == -1)
        {
            clearall();
            return;
        }
        new_connection();
    }
}
/* clear all */
void Form_send::clearall()
{
    ui->order_num->setText("");
    ui->first->setText("");
    ui->again->setText("");
    count_sure = 0;

}
/* box return to send */
void Form_send::box_to_send_Slot()
{
    this->show();
    fbs.hide();
}
/* rewrite */
void Form_send::on_btn_rewrite_clicked()
{
    clearall();
}
/* del */
void Form_send::on_btn_del_clicked()
{
    QString str = ui->order_num->text();
    int len = str.length();
    QString tmp = str.mid(0, len-1);
    ui->order_num->setText(tmp);
}
/* send return to select opt */
void Form_send::on_btn_ret_clicked()
{
    emit return_delivery();
    clearall();
}

void Form_send::on_btn1_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("1"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("1"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("1"));
}

void Form_send::on_btn2_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("2"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("2"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("2"));
}

void Form_send::on_btn3_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("3"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("3"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("3"));
}

void Form_send::on_btn4_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("4"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("4"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("4"));
}

void Form_send::on_btn5_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("5"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("5"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("5"));
}

void Form_send::on_btn6_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("6"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("6"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("6"));
}

void Form_send::on_btn7_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("7"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("7"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("7"));
}

void Form_send::on_btn8_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("8"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("8"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("8"));
}

void Form_send::on_btn9_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("9"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("9"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("9"));
}

void Form_send::on_btn0_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("0"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("0"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("0"));
}

void Form_send::on_q_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("q"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("q"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("q"));
}

void Form_send::on_w_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("w"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("w"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("w"));
}

void Form_send::on_e_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("e"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("e"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("e"));
}

void Form_send::on_r_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("r"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("r"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("r"));
}

void Form_send::on_t_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("t"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("t"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("t"));
}

void Form_send::on_y_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("y"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("y"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("y"));
}

void Form_send::on_u_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("u"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("u"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("u"));
}

void Form_send::on_i_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("i"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("i"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("i"));
}

void Form_send::on_o_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("o"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("o"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("o"));
}

void Form_send::on_p_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("p"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("p"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("p"));
}

void Form_send::on_a_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("a"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("a"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("a"));
}

void Form_send::on_s_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("s"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("s"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("s"));
}

void Form_send::on_d_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("d"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("d"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("d"));
}

void Form_send::on_f_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("f"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("f"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("f"));
}

void Form_send::on_g_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("g"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("g"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("g"));
}

void Form_send::on_h_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("h"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("h"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("h"));
}

void Form_send::on_j_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("j"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("j"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("j"));
}

void Form_send::on_k_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("k"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("k"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("k"));
}

void Form_send::on_l_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("l"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("l"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("l"));
}

void Form_send::on_z_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("z"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("z"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("z"));
}

void Form_send::on_x_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("x"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("x"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("x"));
}

void Form_send::on_c_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("c"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("c"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("c"));
}

void Form_send::on_v_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("v"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("v"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("v"));
}

void Form_send::on_b_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("b"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("b"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("b"));
}

void Form_send::on_n_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("n"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("n"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("n"));
}

void Form_send::on_m_clicked()
{
    if(count_sure == 0)
        ui->order_num->setText(ui->order_num->text().append("m"));
    if(count_sure == 1)
        ui->first->setText(ui->first->text().append("m"));
    if(count_sure == 2)
        ui->again->setText(ui->again->text().append("m"));
}

